package edu.uclmgmu.bacteriosec.aspects.fos;

import edu.uclmgmu.bacteriosec.aspects.common.Store;
import java.io.FileOutputStream;

privileged aspect FOS_OMDuplication {

	void around(java.io.FileOutputStream f, byte[] b) : target(f) && args(b) &&  call(* FileOutputStream.write(byte[])) {
		int numberOfCurrentCall=Store.get(thisJoinPointStaticPart.getSignature());
		int numberOfCallInTurn=Store.getNumberOfCallInTurn();
		if (numberOfCurrentCall==numberOfCallInTurn) {
			Store.log(this.getClass().getCanonicalName(), numberOfCurrentCall, thisJoinPointStaticPart.getSignature());
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);			
			byte[] theArray=new byte[b.length + b.length];
			int i=0;
			for (i=0; i<b.length; i++) {
				theArray[i]=b[i];
			}
			for (int j=i; j<b.length+b.length; j++) {
				theArray[j]=b[j-i];
			}
			b=theArray;
			proceed(f, b);
		} else {
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			proceed(f, b);
		}
	}
	
	void around(java.io.FileOutputStream f, byte[] b, int off, int len) : target(f) && args(b, off, len) &&  call(* FileOutputStream.write(byte[], int, int)) {
		int numberOfCurrentCall=Store.get(thisJoinPointStaticPart.getSignature());
		int numberOfCallInTurn=Store.getNumberOfCallInTurn();
		if (numberOfCurrentCall==numberOfCallInTurn) {
			Store.log(this.getClass().getCanonicalName(), numberOfCurrentCall, thisJoinPointStaticPart.getSignature());			
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			byte[] theArray=new byte[b.length + b.length];
			int i=0;
			for (i=0; i<b.length; i++) {
				theArray[i]=b[i];
			}
			for (int j=i; j<b.length+b.length; j++) {
				theArray[j]=b[j-i];
			}
			b=theArray;
			proceed(f, b, off, len);
		} else {
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			proceed(f, b, off, len);
		}
	}
}
